#!/bin/bash
set +x
tarip=$1
tarver=$2
tardis=$3
tarurs=$4
echo "this is the test script"
#tarip remote ip
#tarver branch 1.0 1.1
#tarurs git address
#tardis test pre deploy t
#tardis deploy=1 test=2 pre=3
echo "$tarip" >> /var/log/nodejs_upload.log
echo "$tarver" >> /var/log/nodejs_upload.log
echo "$tardis" >> /var/log/nodejs_upload.log
echo "$tarurs" >> /var/log/nodejs_upload.log


localop(){
    pname=`ls /home/nodejs/`
    mkdir -p /home/backup
    cd /root/workspace
    bname=`ls -l | grep '^d'|awk '{print $NF}'`
    tar -zcf /home/backup/${dt_bak}_package.tar.gz $bname
    [ $? = 0 ] && echo "Bak Success" >> /var/log/nodejs_upload.log
    [ $? != 0 ] && echo "Bak Failed" >> /var/log/nodejs_upload.log

    kill -9 $(lsof -i:3000 |awk 'NR==2 {print $2}')  > /dev/null 2>&1

    [ $? = 0 ] && echo "nodejs has always killed" >> /var/log/nodejs_upload.log
    [ $? != 0 ] && echo "nodejs doesnot exists，will upload " >> /var/log/nodejs_upload.log

    rm -rf $bname

    [ $? = 0 ] && echo  "rm Success" >> /var/log/nodejs_upload.log
    [ $? != 0 ] && echo "rm Failed" >> /var/log/nodejs_upload.log

    cd /home/nodejs/
    echo "Success"
    cp -r $pname /root/workspace/
    if [ $? -eq 0 ];then

        echo "$logdate nodejs_upload up ok" >> /var/log/nodejs_upload.log
        echo "Success" 
        cd /root/workspace/$pname
        chmod +x main.sh rollback.sh > /dev/null/ 2>&1 &
        nohup node bin/www > myLog.log 2>&1 &
        sleep 5
        
        cd /home/nodejs/
        rm -rf $pname
               
    else
        echo 'Failed'
        echo "$logdate nodejs_upload up failed" >> /var/log/nodejs_upload.log
        cd /home/nodejs/
        rm -rf $pname
        
    fi

}


if [ $tardis == DEPLOY ];then
    key=1
elif [ $tardis == PRE ];then
    key=3
else
    key=2
fi

alias rm="rm"
dt_bak=`date +%Y%m%d_%H%M%S`
dtupload=`date +%Y%m%d`
user_password=Hello1234
logdate=`date +%Y%m%d_%H%M%S`
echo "$dt_bak start run the script !!!" >> /var/log/nodejs_upload.log
IP=`/sbin/ifconfig  | grep inet | awk 'NR==2 {print $2 }' | awk -F ":" '{print $2}'`

mkdir -p /home/nodejs/
cd /home/nodejs/

git clone $tarurs -b $tarver

if [ $? -eq 0 ];then
    echo "GIT successs"  >> /var/log/nodejs_upload.log
else
    echo "GIT Failed" >> /var/log/nodejs_upload.log
    echo "Failed"
    exit 1
fi

#git clone git@git.oschina.net:1hcx/nodejs_ci_customer.git -b $tarver
#git clone git@git.oschina.net:1hcx/nodejs_ci_website.git -b $tarver

sleep 5

pname=`ls /home/nodejs/`

cd /home/nodejs/$pname/routes/
cd /home/nodejs ; chmod +x main.sh rollback.sh
sed -i "11 s/[0-9]/$key/g" httprequest.js  >/dev/null 2>&1

cd /home/nodejs/

if [ $tarip == $IP ]; then
    
    localop
    
    [ $? = 0 ] && echo "Success" 
    [ $? != 0 ] && echo "Failed" 
    
    exit 
    #statements
fi
sshpass -p $user_password ssh root@$tarip "mkdir -p /home/nodejs_upload/"
sshpass -p $user_password scp -r $pname root@$tarip:/home/nodejs_upload/

if [ $? -eq 0 ];then
    echo "SCP to $tarip successs"  >> /var/log/nodejs_upload.log
else
    echo "SCP to $tarip Failed" >> /var/log/nodejs_upload.log
    echo "Failed"
    exit 1
fi
sleep 5

sshpass -p $user_password ssh root@$tarip << \remotessh
set +x
alias rm="rm"
dt_bak=`date +%Y%m%d_%H%M%S`
dtupload=`date +%Y%m%d`
logdate=`date +%Y%m%d_%H%M%S`

echo "$logdate Now Start Operation ==============>" >> /var/log/nodejs_upload.log
#bak
pnamen=`ls /home/nodejs_upload/`
mkdir -p /home/backup
cd /root/workspace
bname=`ls -l | grep '^d'|awk '{print $NF}'`
tar -zcf /home/backup/${dt_bak}_package.tar.gz $bname

[ $? = 0 ] && echo "Bak Success" >> /var/log/nodejs_upload.log
[ $? != 0 ] && echo "Bak Failed" >> /var/log/nodejs_upload.log

#upload

#
#lsof -i:3000 |awk 'NR==2 {print $2}'

kill -9 $(lsof -i:3000 |awk 'NR==2 {print $2}') > /dev/null 2>&1

    [ $? = 0 ] && echo "nodejs has always killed" >> /var/log/nodejs_upload.log
    [ $? != 0 ] && echo "nodejs doesnot exists，will upload " >> /var/log/nodejs_upload.log


#rm
cd /root/workspace

rm -rf $pnamen

[ $? = 0 ] && echo  "rm Success" >> /var/log/nodejs_upload.log
[ $? != 0 ] && echo "rm Failed" >> /var/log/nodejs_upload.log

cd /home/nodejs_upload/

cp -r $pnamen /root/workspace/

if [ $? -eq 0 ];then

    echo "$logdate nodejs_upload up ok" >> /var/log/nodejs_upload.log
    cd /root/workspace/$pnamen
    nohup node bin/www > myLog.log 2>&1 &
    sleep 5
    cd /home/nodejs_upload/;rm -rf *
    # forever start bin/www -l ~/.forever/forever.log -e ~/.forever/err.log -w -a 
    exit 0
else
    echo "$logdate nodejs_upload up failed" >> /var/log/nodejs_upload.log
    cd /home/nodejs_upload/;rm -rf *
    exit 1
fi

remotessh

rpname=`ls /home/nodejs/`

if [ $? -eq 0 ];then
    echo "Success"
    cd /home/nodejs ; rm -rf $rpname
    echo "All Operation is OK" >> /var/log/nodejs_upload.log
else
    echo "Failed"
    cd /home/nodejs ; rm -rf $rpname
    exit 1
fi

